class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        sl = list(s)
        stack = []
        maxl = 0
        for x in sl:
            if x not in stack:
                stack.append(x)
            else:
                i = stack.index(x)
                stack = stack[i+1:]
                stack.append(x)
            maxl = max(len(stack), maxl)
        return maxl

    def lengthOfLongestSubstring_opt(self, s):
        """
        :type s: str
        :rtype: int
        """
        d_map = {}
        maxl = 0
        start = 0
        for i, x in enumerate(s):
            if x in d_map and start <= d_map[x]:
                start = d_map[x] + 1
            else:
                maxl = max(maxl, i-maxl+1)
            d_map[x] = i
        return maxl

s = Solution()
print(s.lengthOfLongestSubstring("   3f3"))